package com.ascentive.finallyfast.deepclean;

import java.util.HashSet;
import java.util.Set;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.ascentive.finallyfast.FinallyFastApplication;

public class SmsInboxLog extends SmsLog {
	
	int noOfMessagesDeleted = 0;

	public SmsInboxLog(FinallyFastApplication app, String name, boolean value, int img) {
		super(app, name, value, img);

	}

	@Override
	void doClean() {
		
		int smsDeleteStatus = 0;
		 // URI for sms
        Uri uriSmsInbox = Uri.parse("content://sms/inbox");
        	
        ContentResolver cr = mApp.getContentResolver();
        Cursor smsInboxCursor = cr.query(uriSmsInbox, new String[] { "thread_id" },
       		 "date <= (" + Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ) +")", null, null);
        
        // if Inbox is Empty
        if(smsInboxCursor.getCount() == 0) 
        	return;
        
        try {
            Set<String> threads = new HashSet<String>();

            while (smsInboxCursor.moveToNext()) {
                threads.add(smsInboxCursor.getString(0));
            }

            for (String thread : threads) {
                // Delete the SMS threads
                String uri = "content://sms/conversations/" + thread;
                smsDeleteStatus = cr.delete(Uri.parse(uri), null, null); 
                noOfMessagesDeleted += smsDeleteStatus; 
                
            }
        } catch (Exception e) {
            Log.e("LOG_TAG", "exception in deleteSms:", e);
        }
		return;
	}
	
	@Override
	int numberOfFileCleaned() {
		return noOfMessagesDeleted;
	}
}
